# -*- coding: utf-8 -*-

'''
需要给前端的数据前面加多c_标识，不能将重要的数据外泄出去
'''

import time
from wxapp.models import ServiceDetail
from wxapp.models import GoodsDetail
from wxapp.models import ShoppingCartDetail


#-------------project bag---------------
from wxapp.base.log import logger

all_cartID = set()


'''
需要给前端的数据前面加多c_标识，不能将重要的数据外泄出去
'''


class ShoppingCartObj:
	global all_cartID
	all_cartID_list = all_cartID
	def __init__(self,cart_id):
		self.cart_id = cart_id
		self.shoppingCartObj = ShoppingCartDetail.objects.get(cart_id=self.cart_id)
		if not self.cart_id in ShoppingCartObj.all_cartID_list:
			logger.info('error:cannot found cart ID %s' % (str(self.cart_id)))

	def get_cartID(self):
		return int(self.cart_id)

	def get_userID(self):
		return str(self.shoppingCartObj.user_id)

	def get_userPhone(self):
		return str(self.shoppingCartObj.user_phone)

	def get_goodsID(self):
		return int(self.shoppingCartObj.goods_id)

	def get_goodsNum(self):
		return float(self.shoppingCartObj.goods_num)

	def get_goodsPrice(self):
		return float(self.shoppingCartObj.goods_price)

	def get_goodsName(self):
		return str(self.shoppingCartObj.goods_name)

	def del_Cart(self):
		if not self.get_goodsNum() == 0:
			self.shoppingCartObj.goods_num = 0
			self.shoppingCartObj.save()
			logger.info('删除%s购物车成功' % (str(self.cart_id)))
		else:
			return None

	def update_cartNum(self,num):
		if not self.get_goodsNum() == 0:
			self.shoppingCartObj.goods_num = int(num)
			self.shoppingCartObj.save()
		else:
			return None





def get_all_cartID():
	global all_cartID
	all_cart = ShoppingCartDetail.objects.all().values('cart_id')
	for cart in all_cart:
		all_cartID.add(int(cart['cart_id']))
	return all_cartID

def add_cart(userID, goodsID,goodsNum,goodsPrice,userPhone=0):
	joinTime = time.ctime()
	obj = ShoppingCartDetail(user_id=userID,goods_id=goodsID,goods_num=goodsNum, \
		goods_price=goodsPrice,join_time=joinTime,user_phone=userPhone)
	obj.save()